﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using KC.Business.Net;

namespace KC.Tests
{
	[TestClass()]
	public class WebTest
	{
		public TestContext TestContext { get; set; }

		[TestMethod()]
		public void BuildListOfImgTagsTest()
		{
			string Html = "<html><blargh flat=\"ulence\"<a href=\"Elmer\"<img src=\"http://www.google.com/google.jpg\" /></a>";
			List<string> result = Web.BuildListOfImgTags(Html, Web.HyperlinkOption.All);
			Assert.AreEqual(1, result.Count);
			Assert.AreEqual("http://www.google.com/google.jpg", result[0]);
		}
		[TestMethod()]
		public void BuildListOfImgTagsInternetTest()
		{
			string Html = "<html><blargh flat=\"ulence\"<a href=\"Elmer\"<img src=\"http://www.google.com/logos/googlebeta.jpg\" /></a>";

			// Test for working links
			List<string> result = Web.BuildListOfImgTags(Html, Web.HyperlinkOption.Working);
			Assert.AreEqual(1, result.Count);
			Assert.AreEqual("http://www.google.com/logos/googlebeta.jpg", result[0]);

			// Test for broken links
			result = Web.BuildListOfImgTags(Html, Web.HyperlinkOption.Broken);
			Assert.AreEqual(0, result.Count);
		}

		[TestMethod()]
		public void GetImagesFromHtmlTest()
		{
			string Html = "<html><blargh flat=\"ulence\"<a href=\"Elmer\"<img src=\"http://www.google.com/logos/googlebeta.jpg\" /></a>";
			List<string> links = new List<string> { "http://www.google.com/logos/googlebeta.jpg" };
			List<string> result = Web.GetImagesFromHtml(Html);

			Assert.AreEqual(links.Count, result.Count);
			Assert.AreEqual(links[0], result[0]);
		}

		[TestMethod()]
		public void GetLinksTest()
		{
			List<string> links = new List<string>() {
				"http://www.google.com/",
				"http://www.example.com",
				"http://asdfasdfadsfa3839.asdfaslhlfhdasfdlasdl8.com"
			};
			List<string> results = Web.GetLinks(links, Web.HyperlinkOption.Broken);
			Assert.AreEqual(1, results.Count);
			Assert.AreEqual(links[2], results[0]);

			results = Web.GetLinks(links, Web.HyperlinkOption.Working);
			Assert.AreEqual(2, results.Count);
			Assert.AreEqual(links[0], results[0]);
			Assert.AreEqual(links[1], results[1]);
		}
		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException),"Links cannot be null")]
		public void GetLinksNullTest()
		{
			Web.GetLinks(null, Web.HyperlinkOption.All);
		}
		[TestMethod]
		public void GetLinksEmptyTest()
		{
			List<string> result = Web.GetLinks(new List<string>(), Web.HyperlinkOption.All);
			Assert.AreEqual(0, result.Count);
		}

		[TestMethod()]
		public void GetLinksFromHtmlTest()
		{
			string Html = "<html><blargh flat=\"ulence\"<a href=\"http://www.google.com\"><img src=\"http://www.google.com/logos/googlebeta.jpg\" /></a>";
			List<string> links = new List<string> { "http://www.google.com" };
			List<string> result = Web.GetLinksFromHtml(Html);

			Assert.AreEqual(links.Count, result.Count);
			Assert.AreEqual(links[0], result[0]);
		}
	}
}
